package org.whatever.aha.zjut.platform.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.whatever.aha.zjut.platform.entity.User;
import org.whatever.aha.zjut.platform.vo.AcademyInfoVo;

import java.util.List;

/**
 * @author Baby_mo
 */
public interface UserMapper extends BaseMapper<User> {

    @Select("select role_name from role where role_id in (select role_id from user_role where user_id = #{loginId})")
    public String[] getRoleById(String loginId);

    @Select("select permission_code from role_permission where role_id in (select role_id from user_role where user_id = #{loginId})")
    public String[] getPermissionById(String loginId);

    @Select("select login_type from user where user_id = #{userId}")
    public int getLoginTypeById(int userId);

    @Select("<script>"
            + "select user.user_id, user.username, user.phone_number, academyAdmin.academy_id, academy.academy_name, user.disabled, user.untie_time  from "
            + "(select * from user where login_type = 3 and deleted = 0) as user "
            + "inner join "
            + "(select * from academy_admin "
            + "<if test='academyIdList.size() != 0 '>"
            + "where academy_id in"
            + "<foreach collection='academyIdList' item='academyId' open='(' separator=',' close=')'>"
            + "#{academyId}"
            + "</foreach>"
            + "</if>"
            + ") as academyAdmin "
            + "on academyAdmin.user_id = user.user_id "
            + "left join "
            + "(select * from academy) as academy "
            + "on academyAdmin.academy_id = academy.academy_id"
            +" order by academy_id DESC limit #{offset}, #{pageSize}"
            +" </script>")
    public List<AcademyInfoVo> getAcademyAccountPageable(@Param("academyIdList") List<Integer> academyIdList,@Param("offset") int offset,@Param("pageSize") int pageSize);

    @Select("<script>"
            + "select count(*) from "
            + "(select * from user where login_type = 3 and deleted = 0) as user "
            + "inner join "
            + "(select * from academy_admin "
            + "<if test='academyIdList.size() != 0 '>"
            + "where academy_id in"
            + "<foreach collection='academyIdList' item='academyId' open='(' separator=',' close=')'>"
            + "#{academyId}"
            + "</foreach>"
            + "</if>"
            + ") as academyAdmin "
            + "on academyAdmin.user_id = user.user_id "
            + "left join "
            + "(select * from academy) as academy "
            + "on academyAdmin.academy_id = academy.academy_id"
            +" </script>")
    public int getAcademyAccountTotal(@Param("academyIdList") List<Integer> academyIdList);

}
